using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace hxSec.StaticPage
{
    public partial class frmBrowser : Form
    {
        private string url;
        private MainForm from;
        private string account;
        private int garphyid;
        private string labelid;
        private string labelname;
        private int width;
        private int height;
        private string filename;
        private string chartformat;

        // 获取数据列表的地址
        public string URL
        {
            get { return url; }
            set { url = value; }
        }

        // 主程序窗口
        public MainForm FROM
        {
            get { return from; }
            set { from = value; }
        }

        // 客户帐号
        public string ACCOUNT
        {
            get { return account; }
            set { account = value; }
        }

        // 客户图片序列号
        public int GRAPHYID
        {
            get { return garphyid; }
            set { garphyid = value; }
        }

        // 标签ID
        public string LABELID
        {
            get { return labelid; }
            set { labelid = value; }
        }

        // 标签名称
        public string LABELNAME
        {
            get { return labelname; }
            set { labelname = value; }
        }

        // 图片宽度
        public int WIDTH
        {
            get { return width; }
            set { width = value; }
        }

        // 文件名称
        public string FILENAME
        {
            get { return filename; }
            set { filename = value; }
        }

        // 图片高度
        public int HEIGHT
        {
            get { return height; }
            set { height = value; }
        }

        // 图片格式
        public string CHARTFORMAT
        {
            get { return chartformat; }
            set { chartformat = value; }
        }

        ChartData chartData = new ChartData();

        // 初始化抓图窗口
        public frmBrowser(string url, string account, int garphyid, string labelid, string labelname, int width, int height, string filename, string chartformat, MainForm from)
        {
            InitializeComponent();
            #region 初始化属性
            URL = url;
            ACCOUNT = account;
            GRAPHYID = garphyid;
            LABELID = labelid;
            LABELNAME = labelname;
            WIDTH = width;
            HEIGHT = height;
            FILENAME = filename;
            CHARTFORMAT = chartformat;
            FROM = from;
            #endregion

            this.webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser_Navigated);
            this.webBrowser.StatusTextChanged += new EventHandler(webBrowser_StatusTextChanged);

            this.txtAddress.Text = URL + "?account=" + ACCOUNT + "&labelid=" + LABELID + "&width=" + WIDTH.ToString() + "&height=" + Height.ToString();
            Go();

            Type type = typeof(frmBrowser);
            this.toolStripStatusLabel.Text += Statistics.VersionVersionInfo;           
        }

        // 重置窗口大小
        void ResetStatusLabelSize()
        {
            int width = this.Width - this.toolStripStatusLabel.Width - 30;
            if (tspBar.Visible)
                width -= this.tspBar.Width;
            this.tssLblInfo.Width = width;
        }

        void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.txtAddress.Text = this.webBrowser.Url.AbsoluteUri;
        }

        void webBrowser_StatusTextChanged(object sender, EventArgs e)
        {
            this.tssLblInfo.Text = this.webBrowser.StatusText;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            Go();           
        }

        private void Go()
        {
            try
            {
                this.webBrowser.Navigate(txtAddress.Text.Trim());
                this.btnSave.Enabled = false;
                this.tspBar.Visible = true;
                ResetStatusLabelSize();
            }
            catch { }
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.btnSave.Enabled = true;
            this.tspBar.Visible = false;
            ResetStatusLabelSize();

            try
            {               
                ImageFormat format = ImageFormat.Jpeg;
                switch (CHARTFORMAT)
                {
                    case "bmp":
                        format = ImageFormat.Bmp;
                        break;
                    case "jpg":
                        format = ImageFormat.Jpeg;
                        break;
                    case "jpeg":
                        format = ImageFormat.Jpeg;
                        break;
                    case "png":
                        format = ImageFormat.Png;
                        break;
                    case "gif":
                        format = ImageFormat.Gif;
                        break;
                    default:
                        format = ImageFormat.Jpeg;
                        break;

                }
                byte[] bytes = FnListChartToModel(ImageFormat.Jpeg);
                if (bytes != null && bytes.Length > 0)
                {
                    GraphyDataInfo graphyData1 = new GraphyDataInfo();
                    graphyData1.ACCOUNTCODE = Convert.ToInt64(ACCOUNT);
                    graphyData1.FILENAME = FILENAME;
                    graphyData1.GRAPHYID = Convert.ToInt64(GRAPHYID);
                    graphyData1.LABELID = Convert.ToInt64(LABELID);
                    graphyData1.LABELNAME = LABELNAME;
                    graphyData1.NR = bytes;
                    graphyData1.OPERATIONDATE = DateTime.Now.ToString();

                    from.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + ACCOUNT + " 生成 " + LABELNAME + "图已完毕");
                    from.queue.Push(graphyData1);
                    chartData.UpdateControlState(GRAPHYID.ToString(), LABELID);
                }               
            }
            catch(Exception ex)
            {
                chartData.UpdateControlTimes(GRAPHYID.ToString(), LABELID);
                from.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + ACCOUNT + " 在生成 " + LABELNAME + " 时出错，错误信息为：\r\n" + ex.ToString());
                from.WriteLog("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + ACCOUNT + " 在生成 " + LABELNAME + " 时出错，错误信息为：\r\n" + ex.ToString());
            }
            finally
            {
                this.Dispose();
            }          
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
        }

        private void webBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            this.tspBar.Maximum = Convert.ToInt32(e.MaximumProgress);
            this.tspBar.Value = Convert.ToInt32(e.CurrentProgress);
        }

        private byte[] FnListChartToModel(ImageFormat format)
        {
            int browserWidth = webBrowser.Width;
            int browserHeight = webBrowser.Height;
            Rectangle rectBody = this.webBrowser.Document.Body.ScrollRectangle;
            int width = rectBody.Width;
            int height = rectBody.Height + 5;
            this.webBrowser.Width = width;
            this.webBrowser.Height = height;
            try
            {
                SnapLibrary.Snapshot snap = new SnapLibrary.Snapshot();
                using (Bitmap bmp = snap.TakeSnapshot(this.webBrowser.ActiveXInstance, new Rectangle(0, 0, width, height)))
                {
                    MemoryStream imgStream = new MemoryStream();
                    switch (CHARTFORMAT)
                    {
                        case "bmp":
                            bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;
                        case "jpg":
                            bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                        case "gif":
                            bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Gif);
                            break;
                        case "png":
                            bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
                            break;
                        default:
                            bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                    }
                    return imgStream.ToArray();
                    //this.webBrowser.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
                    //bmp.Save(path, format);

                    //using (Image img = ImageHelper.GetThumbnailImage(bmp, 800, 600))
                    //{
                    //    ImageHelper.SaveImage(img, path, format);
                    //}

                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return null;
            }
            finally
            {
                this.webBrowser.Width = browserWidth;
                this.webBrowser.Height = browserHeight; 
            }
        }



        private void frmBrowser_Load(object sender, EventArgs e)
        {
            ResetStatusLabelSize();
        }

        private void frmBrowser_Resize(object sender, EventArgs e)
        {
            ResetStatusLabelSize();
        }


    }
}